<%#-- copyright
OpenProject is an open source project management software.
Copyright (C) 2012-2020 the OpenProject GmbH

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License version 3.

OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
Copyright (C) 2006-2017 Jean-Philippe Lang
Copyright (C) 2010-2013 the ChiliProject Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

See docs/COPYRIGHT.rdoc for more details.

++#%>

<% if CostType.exists? %>
  <%# Build a template object for the frontend to add new rows %>
  <% template_object = @cost_object.material_budget_items.build(cost_type: CostType.default) %>

  <costs-budget-subform item-count="<%= @cost_object.material_budget_items.length %>"
                      update-url="<%= url_for(action: :update_material_budget_item, project_id: @project.id) %>">
    <fieldset id="material_budget_items_fieldset" class="form--fieldset -collapsible">
      <legend class="form--fieldset-legend"><%= VariableCostObject.human_attribute_name(:material_budget) %></legend>
        <div class="generic-table--container">
          <div class="generic-table--results-container">
            <table class="generic-table" id="material_budget_items">
              <colgroup>
                <col highlight-col>
                <col highlight-col>
                <col highlight-col>
                <% if User.current.allowed_to?(:view_cost_rates, @project)%>
                  <col highlight-col>
                <%end%>
                <col>
              </colgroup>
            <thead>
              <tr>
                <th class="cost_units">
                  <div class="generic-table--sort-header-outer">
                    <div class="generic-table--sort-header">
                      <span>
                        <%= MaterialBudgetItem.human_attribute_name(:units) %>
                      </span>
                    </div>
                  </div>
                </th>
                <th>
                  <div class="generic-table--sort-header-outer">
                    <div class="generic-table--sort-header">
                      <span>
                        <%= CostType.human_attribute_name(:unit) %>
                      </span>
                    </div>
                  </div>
                </th>
                <th>
                  <div class="generic-table--sort-header-outer">
                    <div class="generic-table--sort-header">
                      <span>
                        <%= MaterialBudgetItem.human_attribute_name(:cost_type) %>
                      </span>
                    </div>
                  </div>
                </th>
                <th>
                  <div class="generic-table--sort-header-outer">
                    <div class="generic-table--sort-header">
                      <span>
                        <%= MaterialBudgetItem.human_attribute_name(:comment) %>
                      </span>
                    </div>
                  </div>
                </th>
                <% if User.current.allowed_to?(:view_cost_rates, @project)%>
                  <th class="currency" id="material_budget_items_price">
                    <div class="generic-table--sort-header-outer">
                      <div class="generic-table--sort-header">
                        <span>
                          <%= MaterialBudgetItem.human_attribute_name(:budget) %>
                        </span>
                      </div>
                    </div>
                  </th>
                <%end%>
                <th></th>
              </tr>
            </thead>
            <tbody id="material_budget_items_body" class="budget-item-container">
              <%= render partial: "cost_objects/items/material_budget_item", object: template_object, locals: { templated: true } %>
              <%- @cost_object.material_budget_items.each_with_index do |material_budget_item, index| -%>
                <%= render partial: 'cost_objects/items/material_budget_item', object: material_budget_item, locals: {index: index} %>
              <%- end -%>
            </tbody>
          </table>

        </div>
      </div>
      <div class="wp-inline-create-button">
        <a class="budget-add-row wp-inline-create--add-link" role="button" title="<%= t(:button_add_budget_item) %>">
          <%= op_icon('icon icon-add') %>
        </a>
      </div>
    </fieldset>
  </costs-budget-subform>
<% end %>
